#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <bitset>
#include <utility>
using namespace std;

#define mm(a, n) memset(a, n, sizeof a)
#define mk(a, b) make_pair(a, b)

const double eps = 1e-6;
const int INF = 0x3f3f3f3f;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef pair<LL, LL> PLL;
typedef pair<int, LL> PIL;

inline void quickread() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

const int N = 110;

int l[N], r[N];
int n;
int nums[N];
vector<int> res;

// 计算 u 节点为根节点的树的总共节点数（包括 u）
int calc_child(int u) {
    if (u == -1) return 0;
    if (l[u] != -1 && r[u] != -1) 
        return calc_child(l[u]) + calc_child(r[u]) + 1;
    if (l[u] != -1 && r[u] == -1)
        return calc_child(l[u]) + 1;
    if (l[u] == -1 && r[u] != -1)
        return calc_child(r[u]) + 1;
    return 1;
}

void BFS(int u) {
    // q.first 为从多少idx开始
    queue<PII> q;
    q.push(mk(0, u));

    while(q.size()) {
        PII head = q.front(); q.pop();
        int idx = head.first;
        int root = head.second;
        if (l[root] != -1) q.push(mk(idx, l[root]));
        if (r[root] != -1) q.push(mk(idx + calc_child(l[root]) + 1, r[root]));
        res.push_back(nums[idx + calc_child(l[root])]);
    }

}

inline void solution() {
    cin >> n;
    for (int i = 0; i < n; i ++ ) {
        int pl, pr;
        cin >> pl >> pr;
        l[i] = pl, r[i] = pr;
    }
    for (int i = 0; i < n; i ++ ) cin >> nums[i];
    sort(nums, nums + n);
    BFS(0);

    cout << res[0];
    for (int i = 1; i < res.size(); i ++ )
        cout << " " << res[i];
    cout << endl;
}

int main() {
    freopen("input.txt", "r", stdin);
    quickread();
    solution();
    return 0;
}